<?php

class ConeccionMssql {
    private static $myServer = "localhost";
    private static $myUser = "sa";
    private static $myPass = "12345";
    private static $myDB = "prueba";  
    
    function __construct() {
        //parent::__construct();
    }
    
    public static function getConexion() {
        //Documentacion: ADO para PHP http://phplens.com/lens/adodb/docs-adodb.htm#execute
        //ADORecordSet: http://phplens.com/lens/adodb/docs-adodb.htm#ADORecordSet
        //create an instance of the  ADO connection object
        $con = new COM ("ADODB.Connection")
                or die("No se pudo iniciar ADO");

        //define connection string, specify database driver
        $conStr = "PROVIDER=SQLOLEDB;SERVER=".ConeccionMssql::$myServer.
                ";UID=".ConeccionMssql::$myUser.
                ";PWD=".ConeccionMssql::$myPass.
                ";DATABASE=".ConeccionMssql::$myDB; 
        
        $con->open($conStr); //Open the connection to the database
        return $con;
    }
}


//ejemplos: //http://webcheatsheet.com/php/connect_mssql_database.php

function ejemplo_mssql() {    
    $myServer = "localhost";
    $myUser = "your_name";
    $myPass = "your_password";
    $myDB = "examples"; 

    //connection to the database
    $dbhandle = mssql_connect($myServer, $myUser, $myPass)
      or die("Couldn't connect to SQL Server on $myServer"); 

    //select a database to work with
    $selected = mssql_select_db($myDB, $dbhandle)
      or die("Couldn't open database $myDB"); 

    //declare the SQL statement that will query the database
    $query = "SELECT id, name, year ";
    $query .= "FROM cars ";
    $query .= "WHERE name='BMW'"; 

    //execute the SQL query and return records
    $result = mssql_query($query);

    $numRows = mssql_num_rows($result); 
    echo "<h1>" . $numRows . " Row" . ($numRows == 1 ? "" : "s") . " Returned </h1>"; 

    //display the results 
    while($row = mssql_fetch_array($result))
    {
      echo "<li>" . $row["id"] . $row["name"] . $row["year"] . "</li>";
    }
    //close the connection
    mssql_close($dbhandle);
}

function ejemplo_ado() {
    $myServer = "localhost";
    $myUser = "your_name";
    $myPass = "your_password";
    $myDB = "examples"; 

    //create an instance of the  ADO connection object
    $conn = new COM ("ADODB.Connection")
      or die("Cannot start ADO");

    //define connection string, specify database driver
    $connStr = "PROVIDER=SQLOLEDB;SERVER=".$myServer.";UID=".$myUser.";PWD=".$myPass.";DATABASE=".$myDB; 
      $conn->open($connStr); //Open the connection to the database

    //declare the SQL statement that will query the database
    $query = "SELECT * FROM cars";

    //execute the SQL statement and return records
    $rs = $conn->execute($query);

    $num_columns = $rs->Fields->Count();
    echo $num_columns . "<br>";  

    for ($i=0; $i < $num_columns; $i++) {
        $fld[$i] = $rs->Fields($i);
    }

    echo "<table>";
    while (!$rs->EOF)  //carry on looping through while there are records
    {
        echo "<tr>";
        for ($i=0; $i < $num_columns; $i++) {
            echo "<td>" . $fld[$i]->value . "</td>";
        }
        echo "</tr>";
        $rs->MoveNext(); //move on to the next record
    }


    echo "</table>";

    //close the connection and recordset objects freeing up resources 
    $rs->Close();
    $conn->Close();

    $rs = null;
    $conn = null;
}


?>
